package OneMax;

import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import BinaryGenotype.*;
import Generic.*;
import IntegerPhenotype.IntegerPhenotype;

public class RandomMax extends EvolutionaryAlgorithm {
	public static final int DEFAULT_ONE_MAX_SIZE = 40;
	private int oneMaxSize;
	
	private IntegerPhenotype goal;

	protected Genotype generateRandomGenotype() {
		Genotype gtype = new BinaryGenotype(oneMaxSize);
		//hello
		gtype.randomize();
		return gtype;
	}
	
	protected Phenotype developPhenotype(Genotype gtype) {
		return new IntegerPhenotype((BinaryGenotype)gtype,1);
	}
	
	protected double calculateFitness(Phenotype ptype) {
		IntegerPhenotype ipType = (IntegerPhenotype)ptype;
		int power = 10;
		int likeness = 0;
		for(int i =0; i<goal.getSize(); i++)
		{
			if(goal.getInt(i)==ipType.getInt(i))
				likeness++;
		}
		
		return Math.pow(likeness,power)/Math.pow(oneMaxSize, power-1);
	}

	protected void performParentSelection() {
		//children = SelectionPolicy.fitnessProportionate(adults, populationSize);
		//children = SelectionPolicy.sigmaScaling(adults, populationSize);
		//children = SelectionPolicy.boltzmannScaling(adults, populationSize,adults.getGeneration());
		children = SelectionPolicy.tournamentSelection(adults, populationSize,5,.01);
	}
	
	
	protected void performCrossover(Genotype gtype1, Genotype gtype2) {
		gtype1.onePointCrossover(gtype2);
	}
	
	protected void performMutation(Genotype genotype) {
		genotype.mutate(mutationRate);
	}
	
	protected void performReplacement() {
		ReplacementProtocol.fullReplacement(children);
	}
	
	
	public double getMaxPossibleFitness() {
		return oneMaxSize;
	}

	protected void initialize(int popSize, double crossoverRate, double mutationRate) {
		oneMaxSize = (int)additionalParameterValues.get(0).doubleValue();
		BinaryGenotype btype = new BinaryGenotype(oneMaxSize);
		btype.randomize();
		goal = (IntegerPhenotype) developPhenotype(btype);
		initializeEvolutionaryAlgorithm(popSize, crossoverRate, mutationRate);
	}
	
	public static void main(String[] args) {
		additionalParameters = new ArrayList<String>();
		additionalParameters.add("Number of bits");
		
		additionalParameterValues = new ArrayList<Double>();
		additionalParameterValues.add((double)DEFAULT_ONE_MAX_SIZE);
		
		additionalStatisticNames = new ArrayList<String>();
		
		EAApplicationFrame2 frame = new EAApplicationFrame2(RandomMax.class);
		frame.setVisible(true);
	}

	public double getAdditionalStatistic(String string) {
		assert false;
		return 0;
	}
	public JPanel getBestIndividualPanel() {
		JPanel ret = new JPanel();
		ret.setLayout(new BoxLayout(ret,BoxLayout.X_AXIS));
		for(Integer i:(IntegerPhenotype)adults.getBestIndividual().getPhenotype())
		{
			ret.add(new JLabel(Integer.toString(i)));
		}
		return ret;
	}
	
	public JPanel getTargetPanel() {
		JPanel ret = new JPanel();
		ret.add(new JLabel("No target"));
		return ret;
	}
}
